package com.abel.main;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import javax.jms.MessageConsumer;

public class MessageConsumer1 {

    public static void main(String[] args) {
        System.out.println("收取到的消息内容： " + getMessage());
    }

    private static String getMessage() {

        ConnectionFactory factory = null;

        Connection connection = null;

        Session session = null;

        Destination destination = null;

        MessageConsumer consumer = null;

        Message message = null;

        String result = "";

        try {
            factory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.26.50:61616");
            connection = factory.createConnection();
            connection.start();

            session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            destination = session.createQueue("first-queue");
            consumer = session.createConsumer(destination);

            message = consumer.receive();
            message.acknowledge();

            result = ((TextMessage)message).getText();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

}
